<template>
  <div>
    <div class="crumbs">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>
                <i class="el-icon-lx-sort"></i> 待审核活动
            </el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="container">
        <div class="handle-box">
            <el-button
                type="primary"
                icon="el-icon-delete"
                class="handle-del mr10"
                @click="delAllSelection"
            >批量删除</el-button>
            <el-button type="primary" icon="el-icon-search" @click="handleALL" style="margin-right:20px;">查询全部</el-button>
                <el-input v-model="query.name" placeholder="活动名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </div> 
                <el-table
                :data="applyList"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="活动ID" width="66" align="center"></el-table-column>
                <el-table-column prop="apply_title" label="活动标题"></el-table-column>
                <el-table-column label="活动图片" align="center" width="180">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.img_url"
                            :preview-src-list="[scope.row.img_url]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="room_bh" label="教室编号" width="90"></el-table-column>
                <el-table-column prop="apply_xz" label="活动类型" width="90"></el-table-column>
                <el-table-column prop="user_name" label="申请账号" width="120"></el-table-column>
                <el-table-column prop="apply_xs" label="活动方式" width="90"></el-table-column>
                <el-table-column prop="time_ks" label="开始时间" width="120"></el-table-column>
                <el-table-column prop="time_jz" label="报名截止" width="120"></el-table-column>
                <el-table-column label="状态" align="center" width="180">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.apply_start==='Y'?'success':(scope.row.apply_start==='N'?'danger':'')"
                        >{{scope.row.apply_start === 'Y'?'已审核':'待审核'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="95" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >审核活动</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="活动名">
                    <el-input v-model="form.apply_title"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="form.apply_start"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data(){
        return {
            applyList:[],
             query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            noticeList:[],
            // datalist:[]
        }
    },
    created(){
        this.getData();
    },    
    methods: {
        handleALL(){
            this.getData(); 
            this.query.name = ''
        },
        getData() {
            this.$http.get('http://118.31.124.52:3000/api/admin/getApplyN').then((result) => {
               this.pageTotal = result.data.res.length
                this.noticeList = result.data.res //内容处理
                this.applyList = this.noticeList.slice(0,this.query.pageSize)   //后期裁剪，制作前端分页
                for(let i = 0;i < this.pageTotal; i ++){
                    this.noticeList[i].time_jz =  this.noticeList[i].time_jz.substring(0,10)
                    this.noticeList[i].time_ks =  this.noticeList[i].time_ks.substring(0,10)
                }         
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.applyList = []
            for(let i = 0;i < this.pageTotal; i ++){
                // str.search("3") != -1
                if(this.noticeList[i].apply_title.indexOf(this.query.name) != -1){
                    this.applyList.push(this.noticeList[i])
                }
            }
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                    console.log(row)
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
            console.log(index,row)
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改成功`);
            this.$http.post('http://118.31.124.52:3000/api/admin/apply-Y',{
                id:this.form.id
            }).then(() => {
                this.applyList.splice(this.idx, 1);         
            })
        },
        // 分页导航
        handlePageChange(val) {
            let start = 0
            let end 
            this.query.pageIndex
            if(val > this.query.pageIndex){
                start = (val-this.query.pageIndex) * this.query.pageSize;
                end = start + this.query.pageSize
            }else{
                end = this.query.pageIndex*this.query.pageSize - (this.query.pageIndex - val)*this.query.pageSize
                start = end - this.query.pageSize;
            }
            this.query.pageIndex = val;
                // this.pageRow
            this.applyList = this.noticeList.slice(start,end)
            
        }
    }
};
</script>

<style>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>